<?php
namespace app\wechat\controller;

use app\wechat\controller\Common;
use think\db;


class User 
{
    //获取部门列表
    public function getDepartment(){

        $common = new Common();
        $param = ['query' => ['access_token' => $common->getAccessToken()] ];

        $department = $common->wechatHttp('/department/list', $param);
        $department = json_decode($department, TRUE);
        if($department['errcode'] == 0){
            return $department['department'];
        }else{
            return false ;
        }
        
    }
    
    //根据部门id获取详情
    public function getUserList($department_id){
        
        $common = new Common();
        $param = ['query' => ['access_token' => $common->getAccessToken(), 'department_id' => $department_id, 'fetch_child' => 1 ] ];
        
        $userlist = $common->wechatHttp('/user/list', $param);
        $userlist = json_decode($userlist, TRUE);
        if($userlist['errcode'] == 0){
            return $userlist['userlist'];
        }else{
            return false ;
        }
        
    }
    
    //获取所有用户
    public function getAllUserList(){
        
        return $this->getUserList(1);
		
    }
    
    //获取所有标签
    public function getAllTag(){
        $common = new Common();
        $param = ['query' => ['access_token' => $common->getAccessToken()] ];
        
        $taglist = $common->wechatHttp('/tag/list', $param);
        $taglist = json_decode($taglist, TRUE);
        if($taglist['errcode'] == 0){
            return $taglist['taglist'];
        }else{
            return false ;
        }
    }
    
    //根据标签id获取详情
    public function getTagUserList($tagid){
        
        $common = new Common();
        $param = ['query' => ['access_token' => $common->getAccessToken(), 'tagid' => $tagid] ];
        
        $userlist = $common->wechatHttp('/tag/get', $param);
        $userlist = json_decode($userlist, TRUE);
        if($userlist['errcode'] == 0){
            return $userlist['userlist'];
        }else{
            return false ;
        }
        
    }
    
    //根据部门id获取用户
    public function batchUserList($list = [], $unique = TRUE){
        $common = new Common();
        //$list = $this->getDepartment();
        $access_token = $common->getAccessToken();
        $params = [];
        $this->userlist = [];
        $this->params = [];
        foreach($list as $v){
            $this->params[] = $param = ['access_token' => $access_token, 'department_id' => $v, 'fetch_child' => 1];
            $params[] = http_build_query($param);
        }
        $callback = function($list, $index){
            $list = json_decode($list, TRUE);
            if($list['errcode'] == 0){
                if($list['userlist']){
                    $this->userlist[$this->params[$index]['department_id']] = $list['userlist'];
                } 
            }
        };
        $common->poolHttp('/user/list', $params, $callback);
        //print_r("<pre>");
        //print_r($this->userlist);
        if($unique){
            return $common->getArrayUniqueByKeys($this->userlist,'userid');
        }
        
        return $this->userlist;
    }
    
    //根据标签id获取用户
    public function batchTagUserList($list = []){
        $common = new Common();
        //$list = $this->getDepartment();
        $access_token = $common->getAccessToken();
        $params = [];
        $this->userlist = [];
        $this->params = [];
        foreach($list as $v){
            $this->params[] = $param = ['access_token' => $access_token, 'tagid' => $v];
            $params[] = http_build_query($param);
        }
        $callback = function($list, $index){
            $list = json_decode($list, TRUE);
            if($list['errcode'] == 0){
                if($list['userlist']){
                    $this->userlist[$this->params[$index]['tagid']] = $list['userlist'];
                }    
            }
        };
        $common->poolHttp('/tag/get', $params, $callback);
        //print_r("<pre>");
        //print_r($this->userlist);
        
        return $this->userlist;
    }
    
    //授权登录
    //参数 : $param = 'user/profile' 
    public function login($param, $STATE = 'STATE'){

        $Agent = $_SERVER['HTTP_USER_AGENT'];

        if(ereg('webdebugger', $Agent)){
            $userid = 'hyuser';
        
            $userQuery = Db::name("member");
            $result = $userQuery->where('openid', $userid)->find();
            session('user',$result);
            
            $targetUrl =  '/' ;
            
            
            header('location:'. $targetUrl); // 跳转到 user/profile
            return ;
        }
        

        /*$userid = '101010534';
        $userQuery = Db::name("member");
        $result = $userQuery->where('openid', $userid)->find();
        session('user',$result);
        
        $targetUrl =  '/' ;
        
        
        header('location:'. $targetUrl); // 跳转到 user/profile
        return ;*/
        

        

        if (empty($_SESSION['wechat_user'])) {

            $_SESSION['oauth_target_url'] = $param;
            
            $common = new Common(); 
            header("Location:".$common->getOauthUrl($STATE));
            return ;
        }

        $user = $_SESSION['wechat_user'];
        //print_r($user);die();
        return $user;
    }
    //发送消息 文本卡片消息类型
    public function send($user,$title,$descr,$url){
        $common = new Common();
        $access_token = $common->getAccessToken();
        $param =[
            'query'=>['access_token' => $access_token ],
            'json' =>[
                "touser" => $user,
                "msgtype" => "textcard",
                "agentid" =>1000005,
                "textcard" => [
                        "title"=> $title,
                        "description"=> $descr,
                        "url"=> $url,
                ]
            ]
            
        ];
        $result = $common->wechatHttp('/message/send', $param,'POST');
        $result = json_decode($result, TRUE);
        if($result['errcode'] == 0){
            return true;
        }else{
            return false;
        }
    }
    //发送消息 文本卡片消息类型
    public function send_text($part='',$user='',$content){
        $common = new Common();
        $access_token = $common->getAccessToken();
        $param =[
            'query'=>['access_token' => $access_token ],
            'json' =>[
                "toparty"=>$part,
                "touser" => $user,
                "msgtype" => "text",
                "agentid" =>1000005,
                "text" => [
                    "content"=> $content,
                ]
                
            ]
        ];
        $result = $common->wechatHttp('/message/send', $param,'POST');
        $result = json_decode($result, TRUE);
        if($result['errcode'] == 0){
            return true;
        }else{
            return false;
        }
    }
}
